package com.da.javatest.leetCode2;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Code599 {
    public static void main(String[] args) {
        new Code599().findRestaurant(new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"},
                new String[]{"KFC", "Shogun", "Burger King"});
    }

    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String, Integer> map = new HashMap<>();

        for (int i = 0; i < list1.length; i++) {
            for (int j = 0; j < list2.length; j++) {
                if (list1[i].equals(list2[j])) {
                    map.put(list1[i], i + j);
                }
            }
        }

        Integer i = map.values().stream().sorted().findFirst().get();
        System.out.println(i);
        List<String> collect = map.entrySet().stream().filter(e -> e.getValue().equals(i))
                .map(Map.Entry::getKey).collect(Collectors.toList());
        String[] strs = new String[collect.size()];
        collect.toArray(strs);
        return strs;
    }
}
